import CustomButton from "../../../Script/Common/CustomButton";
import { EBundleType } from "../../../Script/Common/Enum";
import EventID from "../../../Script/Common/EventID";
import { GlobalData } from "../../../Script/Common/GlobalData";
import { TalkModule } from "../../../Script/Common/TalkModule";
import { IBaseSkeData, IGameData, IMessage, ITalkData, ITaskData } from "../../../Script/Interface/IMessage";
import { SceneGame } from "../../../Script/SceneGame";
import GameUtil from "../../../Script/Util/GameUtil";
import { LoadUtil } from "../../../Script/Util/LoadUtil";
import StorageUtil from "../../../Script/Util/StorageUtil";
import BaseLayer from "../../Base/BaseLayer";
import TaskModule from "../../Common/TaskModule";
import CartoonUI from "./CartoonUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HomeUI extends BaseLayer implements IMessage {
    public static instance: HomeUI = null;

    @property({ type: [cc.Node], tooltip: "clicks" })
    private clicks: cc.Node[] = [];
    @property({ type: cc.Node, tooltip: "墙节点" })
    private qiang: cc.Node = null;

    @property({ type: cc.Node, tooltip: "扎针按钮" })
    private btndazhen: cc.Node = null;
    @property({ type: cc.Node, tooltip: "修理按钮" })
    private btnRepair: cc.Node = null;
    @property({ type: CustomButton, tooltip: "顾母" })
    private btnGumu: CustomButton = null;
    @property({ type: sp.Skeleton, tooltip: "顾母躺床上动画" })
    private gumuSke: sp.Skeleton = null;
    @property({ type: sp.Skeleton, tooltip: "顾母站床边动画" })
    private gumuZhanSke: sp.Skeleton = null;
    @property({ type: sp.Skeleton, tooltip: "林瑶站在屋里动画" })
    private linYaoSke: sp.Skeleton = null;


    @property({ type: cc.Node, tooltip: "获取手镯时引导缩圈位置" })
    private guideShouZhuo: cc.Node = null;

    private gameData: IGameData = null;
    private talkData: ITalkData = null;
    private taskData: ITaskData = null;

    recvMsg(cmd: number, data: any) {
        switch (cmd) {
            case EventID.TEST_CMD:
                {

                }
                break;
        }
    }

    protected onLoad() {
        super.onLoad();
        HomeUI.instance = this;

        this.gameData = StorageUtil.instance.getGameData();
        this.talkData = StorageUtil.instance.getTalkData();
        this.taskData = StorageUtil.instance.getTaskData();
    }

    protected onEnable(): void {
        this.check();
    }

    public check() {
        this.updateGumu();
        this.updateHome();
        this.updateLine();
        this.updataDaZhen();
        this.showCartoon();
    }

    private updateLine() {
        switch (this.talkData.id) {
            case 10107:
                this.talkData.id = 10201;
                TalkModule.updateCheck();
                break;
            case 10552:
                this.talkData.id = 10601;
                TalkModule.updateCheck();
                break;
            case 10806:
                // 医馆学徒
                const hospitalData = StorageUtil.instance.getHospitalData();
                if (hospitalData.id >= 3) {
                    this.talkData.id = 10900;
                    TalkModule.updateCheck();
                }
                break;
        }
    }

    public showLaJiGuide() {
        const taskData = StorageUtil.instance.getTaskData();
        if (taskData.id === 1) {
            const node = this.clicks[0];
            const bad = cc.find("bad", node);
            if (bad.active) {
                SceneGame.instance.showGuideUI(this.clicks[0], 0.8);
            }
        }
    }

    /** 出现打针按钮? */
    private updataDaZhen() {
        this.btndazhen.active = this.talkData.id === 10903;
    }

    /** 展示漫画 */
    private showCartoon() {
        if (this.talkData.id === 10412) {
            // 通过弹窗的方式添加会展示UI层，所以单独处理
            LoadUtil.instance.loadRes(EBundleType.Game, 'prefab/panel/CartoonUI', cc.Prefab, (err, res) => {
                const node = cc.instantiate(res) as cc.Node;
                node.parent = SceneGame.instance.getToastNode();
                node.getComponent(CartoonUI).init(1, () => {
                    // 衔接剧情
                    const talkData = StorageUtil.instance.getTalkData();
                    talkData.id = 10500;
                    TalkModule.updateCheck();
                });
                console.log("漫画this.talkData.id:", this.talkData.id);
            });
        }
    }

    private updateHome() {
        // 是否可清理垃圾/老鼠/桌子
        this.clicks.forEach((o) => {
            o.getComponent(CustomButton).enabled = this.talkData.id === 10005;
            // 设置成好的状态
            if (this.talkData.id >= TalkModule.OPEN_CHAT) {
                this.showOrHide(o);
            }
        });
        // 出现修墙按钮
        const isHad = GlobalData.checkBagHad(10002);
        if (isHad && this.talkData.id === 10205) {
            this.btnRepair.active = true;
            SceneGame.instance.showGuideUI(this.btnRepair);
        } else if (this.talkData.id >= 10301) {
            // 设置墙节点
            this.showOrHide(this.qiang);
        }
    }

    /** 切换顾母人物 */
    private updateGumu() {
        // 在屋里显示林瑶
        this.linYaoSke.node.active = this.talkData.id >= 10500 && this.talkData.id < 10510;
        // 顾母设置能可点击
        this.setGuMuEnabled(this.talkData.id === TalkModule.OPEN_MARKET);
        // 顾母节点显示
        this.gumuSke.node.active = false;
        this.gumuZhanSke.node.active = false;
        if (this.talkData.id < 10005) {
            this.gumuZhanSke.node.active = true;
            // 肩膀手不显示
            this.gumuZhanSke.findBone("hand2").active = false;
        } else {
            this.gumuSke.node.active = true;
            const data: IBaseSkeData = {
                ske: this.gumuSke,
                animName: "ma_idle2",
                isLoop: true,
                hideBones: ["hand2"],
            }
            GameUtil.playAnimation(data);
        }
    }

    private showOrHide(node: cc.Node) {
        const good = cc.find("good", node);
        const bad = cc.find("bad", node);
        if (good) good.active = true;
        if (bad) bad.active = false;
    }

    private setGuMuEnabled(enabled: boolean) {
        // 顾母提示小手展示
        this.btnGumu.enabled = enabled;
        const hand = cc.find('hand', this.btnGumu.node);
        hand.active = this.btnGumu.enabled;
    }

    private completes: number[] = [0, 0, 0];
    private async onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const name = event.target.name;
        switch (name) {
            case 'btnTip':
                {

                }
                break;
            case 'laji':
            case 'guizi':
            case 'table':
                {
                    // 关闭引导
                    SceneGame.instance.hideGuideUI();

                    for (let i = 0; i < this.clicks.length; i++) {
                        const node = this.clicks[i];
                        if (node.name === name) {
                            const comp = node.getComponent(CustomButton);
                            if (!comp.enabled) return;
                            comp.enabled = false;
                            this.completes[i] = 1;
                            this.showOrHide(node);
                            break;
                        }
                    }

                    let isComplete = true;
                    for (let i = 0; i < this.completes.length; i++) {
                        if (this.completes[i] != 1) {
                            isComplete = false;
                            break;
                        }
                    }

                    // 打扫完成
                    if (isComplete) {
                        // 更新对话
                        this.talkData.id = 10101;
                        TalkModule.updateCheck();
                        // 更新任务数据
                        TaskModule.instance.saveData();
                    }
                }
                break;
            case 'btnRepair':
                {
                    if (this.btnRepair.active) {
                        // 墙修理完成
                        this.btnRepair.active = false;
                        // 设置墙节点
                        this.showOrHide(this.qiang);
                        // 从背包删除一个铁板
                        GlobalData.removeBagData(10002, 1);
                        // 关闭引导层
                        SceneGame.instance.hideGuideUI();

                        // 更新对话内容
                        this.talkData.id = 10301;
                        TalkModule.updateCheck();
                        // 更新任务数据
                        TaskModule.instance.saveData();
                    }
                }
                break;
            case 'btnGumu':
                {
                    if (!this.btnGumu.enabled) {
                        return;
                    }
                    this.setGuMuEnabled(false);
                    // 已领取手镯
                    this.talkData.id = 10305;
                    // 获得奖励
                    SceneGame.instance.showPopView("ItemUI", { itemId: 10000, num: 1 });
                    // 引导缩圈(在ItemUI里位置不对)
                    await GameUtil.sleep(300);
                    SceneGame.instance.showGuideUI(this.guideShouZhuo);
                }
                break;
            case 'btndazhen':
                {
                    LoadUtil.instance.loadRes(EBundleType.Game, 'prefab/panel/SyringeUI', cc.Prefab, (err, res) => {
                        const node = cc.instantiate(res) as cc.Node;
                        node.parent = SceneGame.instance.getToastNode();
                        console.log("打针:", this.talkData.id);
                    });
                }
                break;
        }
    }
}